package mop.controller;

import java.util.LinkedList;
import java.util.List;

import mop.exception.IllegalDescriptionException;
import mop.exception.IllegalDuplicationException;
import mop.exception.IllegalIdException;
import mop.model.World;
import mop.model.user.User;
import mop.model.user.UserInterface;
import mop.model.user.UserType;
import mop.model.user.UserTypeInterface;

/**
 * CreateUserController
 * 
 * Use case controller for the creation of a new user. 
 * (CUSTOM USE CASE)
 * 
 * @author	MOP Team 7
 */
public class CreateUserController extends UseCaseController
{

	/**
	 * @see UseCaseController#UseCaseController(World)
	 */
	public CreateUserController(World world)
	{
		super(world);
	}
	
	/**
	 * Returns a list of UserType interfaces
	 * 
	 * @return list of usertype interfaces
	 */
	public List<UserTypeInterface> getUserTypes()
	{
		List<UserType> l = super.getWorld().getUserTypeDataBase().fetchAll();
		if(l == null) return null;
		
		List<UserTypeInterface> r = new LinkedList<UserTypeInterface>();
		for(UserType u : l)
		{
			r.add(u);
		}
		return r;
	}
	
	/**
	 * CUSTOM Use case : Create a user
	 * 
	 * @param   ut UserTypeInterface to the usertype
	 * @param	name
	 * 			The username of the new user.
	 * @post	The Project is added to the Project database.
	 * 			| projectDatabase.contains(this)
	 * @throws  IllegalDescriptionException
	 * 			Thrown when the Project description is invalid.
	 * @throws 	IllegalDuplicationException 
	 * 			Thrown when a Project with the same description already exists in the database.
	 */
	public UserInterface createUser(UserTypeInterface ut,String name) 
		throws IllegalDuplicationException, IllegalDescriptionException,IllegalIdException
	{
		UserType uu = super.getWorld().getUserTypeDataBase().find(ut.getId());
		User newUser = new User(uu,name,getWorld().getUserDataBase());
		return newUser;
	}
	
	/***************************************************************************
	 * 								Visit
	 ***************************************************************************/
	
	/**
	 * Make a CallBack to the visitor
	 * @param v the visitor
	 */
	public void visit(ControllerVisitor v)
	{
		v.action(this);
	}
	
}